
import {legacy_createStore,applyMiddleware} from 'redux'
import logger from 'redux-logger'
import thunk from 'redux-thunk'
import {ALLTYPELIST,SETLIST,ALLACTION,ADDLISTDATA,GETCHILDERLIST,INDEXDATA} from '../types/store.d'

const initialState = {
    list:[],
    childrenlist:[],
    indexdata:[],
    id: '',
    xiangq:[],
    title:[]
}

const reducer = (state:ALLTYPELIST = initialState, { type, payload }:ALLACTION) => {
  switch (type) {

  case SETLIST:
    state.list = payload
    state.childrenlist = payload[0].children
    
    return { ...state}

  case GETCHILDERLIST:
    state.childrenlist = state.list[payload].children
    return { ...state }

  case INDEXDATA:
    state.indexdata=payload
    return { ...state }

  case ADDLISTDATA:
    // const newlist = []  //深拷贝
    
    // newlist.push(payload)
    // console.log(payload,21345);
    
    state.xiangq.unshift(payload)
    return { ...state }




  default:
    return state
  }
}


const store = legacy_createStore(reducer,applyMiddleware(logger,thunk))

export default store
